/*
 * Software for Interactive Project Pool Game
 * Columbia University. New York, 2008
 * Authors:
 *   Abdulhamid Ghandour
 *   Thomas John
 *   Jaime Peretzman
 *   Bharadwaj Vellore
 * 
 * Desc:
 */
#include <io.h>
#include <system.h>
#include <stdio.h>
#include "fixedpoint.h"
long long tableStartX;
long long tableEndX;
long long tableStartY;
long long tableEndY;

#define IOWR_LED_DATA(base, offset, data) IOWR_16DIRECT(base, (offset) * 2, data) 
#define IORD_LED_DATA(base, offset) IORD_16DIRECT(base, (offset) * 2)
#define IOWR_LED_SPEED(base, data) IOWR_16DIRECT(base + 32, 0, data)
#define IOWR_POS(base, offset, data) IOWR_16DIRECT(base, (offset) * 2,data)
#define IORD_FLAG (base, offset) IORD_16DIRECT(base, (offset) * 2)
#define NO_CUE_DETECTED (IORD_32DIRECT(VISION_BASE, 0) >> 31)
  
void wait_fn(){
  int i=0;
  int j=0;
  for (;i<=4001;){
    i++;
  }
  while((IORD_16DIRECT(VGA_BASE, 12*2) & 0x0001) ==1);
  for(;j<=500;){
    j++;
  }
}
    
void wait_fn2(){
  int i=0;
  for (;i<=150001;){
    i++;
  }
  while((IORD_16DIRECT(VGA_BASE, 20*2) & 0x0001) ==1);
}
    
/*void wait_fn2(){
  int i=0;
  int j=0;
  for (;i<=50001;){
    i++;
  }
  while((IORD_16DIRECT(VGA_BASE, 20*2) & 0x0001) ==1);
  for(;j<=500;){
    j++;
  }
}*/
    
void wait_fn3(){
  int i=0;
  for (;i<=5000001;){
    i++;
  }
}
         
void wait_fn4(){
  int i=0;
  for (;i<=4001;){
    i++;
  }
}

void wait_fn5(){
  int i=0;
  for(;i<=600001;){
    i++;
  }
}

int calibrate(){
  int cross_H = 150;
  int cross_V = 150;
  int x1 = 0;
  int x2 = 320;
  int y1 = 140;
  int y2 = 180;
  int temp_x1=0, temp_x2=0, temp_y1=0,temp_y2=0;
  int cal_flag=0;
  int read_in=0;
  int delta_stick = 4; // Defines the stepsize in the calibration sticks
  int border_margin = 15;

  int xin, yin;

  while (1) {
    wait_fn2();
   //printf("H = %d, V = %d\n",cross_H,cross_V);
    IOWR_POS(VGA_BASE, 16, x1);
    IOWR_POS(VGA_BASE, 18, x2);
    IOWR_POS(VGA_BASE, 17, y1);
    IOWR_POS(VGA_BASE, 19, y2);
    IOWR_POS(VGA_BASE, 21, 0);
    IOWR_POS(VGA_BASE, 13, cross_H);
    IOWR_POS(VGA_BASE, 14, cross_V);
    wait_fn4 ();
    xin = IORD_32DIRECT(VISION_BASE, 0);
    yin = (xin >> 16) & 0x00007FFF;
    xin = xin & 0x0000FFFF;
    
    cross_H = xin;
    cross_V = yin;
    
    read_in+=1;
    wait_fn2();
  
    if (cal_flag==0&& read_in>2)
    x2 = x2 - delta_stick;
    
    if(NO_CUE_DETECTED && cal_flag==0 && read_in>2)
    {   
        wait_fn3();
        temp_x1 = x2 + delta_stick;
        x2=639;
        x1=320;
        y1=140;
        y2=180;
        cal_flag=1;
        read_in=0;
    }
    if (cal_flag==1 && read_in>2)
    x1 = x1 + delta_stick;
    
    if(NO_CUE_DETECTED  && cal_flag==1 && read_in>2)
    {    
         wait_fn3();
         temp_x2 = x1 - delta_stick;
         x1= 320;
         x2= 380;
         y1=0;
         y2=240;
         cal_flag=2;
         read_in=0;    
    }
    if (cal_flag==2 && read_in>2)
    y2 = y2 - delta_stick;
    
    if(NO_CUE_DETECTED  && cal_flag==2 && read_in>2)
    {   
        wait_fn3();
        temp_y1 = y2 + delta_stick;
        x1=120;
        x2=180;
        y1=240;
        y2=480;
        cal_flag=3;
        read_in=0;
    }
    if (cal_flag==3 && read_in>2)
    {
      y1 = y1 + delta_stick;
    }
    
    if(NO_CUE_DETECTED  && cal_flag==3 && read_in>2)
    {
      temp_y2 = y1 - delta_stick;
      tableStartX = (long long)temp_x1 + border_margin;
      tableEndX = (long long)temp_x2 - border_margin;
      tableStartY = (long long)temp_y1 + border_margin;
      tableEndY = (long long)temp_y2 - border_margin;
      IOWR_POS(VGA_BASE, 16,tableStartX);
      IOWR_POS(VGA_BASE, 18,tableEndX );
      IOWR_POS(VGA_BASE, 17, tableStartY);
      IOWR_POS(VGA_BASE, 19, tableEndY);
      IOWR_POS(VGA_BASE, 21, 1);
      break;
    }
  }
  return 0;
}


